package com.adgox.tiantianbiting.listen.find_like;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.RecommendAdapter;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.home.programDetail.ProgramDetailActivity;
import com.adgox.tiantianbiting.main.MainActivity;
import com.adgox.tiantianbiting.mine.login.LoginActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindLikeActivity extends AppCompatActivity implements RecommendAdapter.onItemClickListener {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.ll_tips)
    LinearLayout llTips;
    private int categoryId;
    private int userId;
    private List<SubProgramBean> datas;
    private RecommendAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_like);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        categoryId = intent.getIntExtra("categoryId", -1);
        if (!SessionManager.getDefault().isLogin()) {
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }
        if (categoryId <= 0) {
            Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
            return;
        }
        userId = ((UserInfo) SessionManager.getDefault().getUser()).getId();
        init();
    }

    private void init() {
        DialogUtils.showLoadingDialog(this);
        OkGo.<BaseResponseBean<List<SubProgramBean>>>get(ApiConstant.BASE_URL + ApiConstant.FIND_LIKE)
                .params("userId", userId)
                .params("categoryId", categoryId)
                .execute(new JsonCallback<BaseResponseBean<List<SubProgramBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<SubProgramBean>>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())) {
                            Toast.makeText(FindLikeActivity.this, response.body().getMessage() != null ? response.body().getMessage() :
                                    "数据获取错误", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (response.body().getData() == null || response.body().getData().size() == 0) {
                            Toast.makeText(FindLikeActivity.this, "没有相关节目", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        setData(response.body().getData());
                    }
                });
    }

    private void setData(List<SubProgramBean> data) {
        datas = data;
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        adapter = new RecommendAdapter(data);
        adapter.setOnItemClickListener(this);
        rv.setAdapter(adapter);
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }

    @OnClick(R.id.tv_jump)
    public void onJumpClicked() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("flg","listen");
        startActivity(intent);
    }


    @Override
    public void onItemClick(View view, RecommendAdapter.ViewName viewName, int position) {
        if (viewName == RecommendAdapter.ViewName.COLLECT) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("programId", datas.get(position).getId());
            map.put("status", 2);
            map.put("seasonId", 1);
            map.put("userId", userId);
            Gson gson = new Gson();
            String s = gson.toJson(map);
            if (datas.get(position).getMarkStatus() == 0) {
                //取消收藏
                delCollect(s);
            } else {
                //收藏
                addCollect(s);
            }
        } else if (viewName == RecommendAdapter.ViewName.ITEM) {
            Intent intent = new Intent(this, ProgramDetailActivity.class);
            intent.putExtra("programId", datas.get(position).getId());
            intent.putExtra("programName", datas.get(position).getTitle());
            startActivity(intent);
        }
    }

    private void addCollect(String s) {
        DialogUtils.showLoadingDialog(this);
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.ADD_COLLECT_AND_HISTORY)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())) {
                            datas.clear();
                            if (adapter != null) adapter.clear();
                            init();
                            showTips();
                        } else {
                            Toast.makeText(FindLikeActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void showTips() {
        llTips.setVisibility(View.VISIBLE);
        AlphaAnimation animation = new AlphaAnimation(1f, 0f);
        animation.setRepeatCount(0);
        animation.setDuration(5000);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                llTips.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        llTips.setAnimation(animation);
    }

    private void delCollect(String s) {
        DialogUtils.showLoadingDialog(this);
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.CANCEL_COLLECT)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())) {
                            datas.clear();
                            if (adapter != null) adapter.clear();
                            init();
                        } else {
                            Toast.makeText(FindLikeActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}
